package main

import (
	"crontab/worker"
	"flag"
	"log"
	"os"
	"time"
)

var (
	configFile string
)

// 初始化参数
func initArgs() {
	flag.StringVar(&configFile,"config","./worker/worker.json","传入worker.json的配置文件路径")
	flag.Parse()
}

func main() {
	var (
		err error
	)

	initArgs()
	err = worker.InitConfig(configFile)
	if err != nil {
		path,_ := os.Getwd()
		log.Printf("当前目录 " + path)
		log.Printf("初始化config配置失败" + err.Error())
		return
	}

	err = worker.InitEtcd()
	if err != nil {
		log.Printf("初始化etcd失败 " + err.Error())
		return
	}
	// 启动mongoDb
	if err = worker.InitLogSkin(); err != nil {
		log.Printf("初始化mongoDb失败:" + err.Error())
		return
	}

	// 启动执行器
	worker.InitExecutor()
	// 启动调度协程
	worker.InitScheduler()
	// 启动监听
	worker.G_EtcdJob.WatchJob()
	// 启动强杀任务监听
	worker.G_EtcdJob.WatchKills()
	// ip注册etcd
	if err = worker.InitRegister(); err != nil {
		log.Printf("服务注册失败,错误信息为:" + err.Error())
		return
	}

	for {
		time.Sleep(time.Second * 60)
	}
}
